package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Category;
import csu.web.mypetstore.domain.Product;
import csu.web.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class CategoryFormServlet extends HttpServlet {

    private CatalogService catalogService;
    private static  final  String CATEGORY_FORM = "/WEB-INF/jsp/catalog/category.jsp";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
        * 在category.jsp中查需要获取的东西
        * 在main.jsp中通过web.xml的映射跳转并传值
        * 需要注意main.jsp的值要和servlet还有数据库中保持一致
        * servlet接收到这个值并通过service到数据库中找到对应的结果
        *将查询结果存储在session中
        * 注意session存储内容要和category.jsp命名保持一致
        * 最后跳到CATEGORY_FORM
        * */
        String categoryId = req.getParameter("categoryId");
        catalogService = new CatalogService();
        Category category = catalogService.getCategory(categoryId);
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        HttpSession session = req.getSession();
        session.setAttribute("category",category);
        session.setAttribute("productList",productList);
        req.getRequestDispatcher(CATEGORY_FORM).forward(req,resp);
    }
}
